package com.tongtech.system.service.impl;

import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.FilePermission;
import com.tongtech.system.mapper.FilePermissionMapper;
import com.tongtech.system.service.IFilePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件权限管理 服务实现
 *
 * @author xingyc
 */
@Service
public class FilePermissionServiceImpl implements IFilePermissionService {
    @Autowired
    private FilePermissionMapper filePermissionMapper;


    /**
     * 查询文件权限管理数据
     *
     * @param filePermission 文件权限信息
     * @return 文件权限信息集合
     */
    @Override
    public List<FilePermission> selectFilePermissionList(FilePermission filePermission) {
        List<FilePermission> list = filePermissionMapper.selectFilePermissionList(filePermission);
        if(CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }

    @Override
    public List<FilePermission> selectSharedList(FilePermission filePermission) {
        List<FilePermission> list = filePermissionMapper.selectSharedList(filePermission);
        if(CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }

    /**
     * 新增保存文件权限信息
     *
     * @param filePermission 文件权限信息
     * @return 结果
     */
    @Override
    public int insertFilePermission(FilePermission filePermission) {
        int result = filePermissionMapper.insertFilePermission(filePermission);
        return result;
    }

    /**
     * 批量新增保存文件权限信息
     *
     * @param filePermissions 文件权限信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertFilePermissionBatch(List<FilePermission> filePermissions) {
        int result = 0;
        for (FilePermission permission : filePermissions) {
            //先查询是否已经存在于分享列表
            FilePermission queryPermission = new FilePermission();
            Long fileId = permission.getFileId();
            String userName = permission.getUserName();
            queryPermission.setFileId(fileId);
            queryPermission.setUserName(userName);
            List<FilePermission> permissions = selectFilePermissionList(queryPermission);
            if (CollectionUtils.isEmpty(permissions)) {
                permission.setCreateBy(SecurityUtils.getUsername());
                filePermissionMapper.insertFilePermission(permission);
            } else {
                permission.setUpdateBy(SecurityUtils.getUsername());
                filePermissionMapper.updateFilePermission(permission);
            }
            result++;
        }
        return result;
    }

    /**
     * 删除文件权限管理信息
     *
     * @param fileId   文件ID
     * @param userName 用户名
     * @return 结果
     */
    @Override
    public int deleteById(Long id) {

        return filePermissionMapper.deleteById(id);
    }
}
